﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using AteneoLibriBusinessLogic.Entities;
using System.Web.UI.WebControls;

namespace AteneoLibri
{
    public partial class UserDetails : System.Web.UI.Page
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (User.Identity.IsAuthenticated)
            {
                if (User.Identity.Name.Equals(System.Configuration.ConfigurationManager.AppSettings["AdminName"]))
                {
                    Page.MasterPageFile = "~/BackEnd/BackEndMasterPage.Master";
                }
                else
                {
                    Page.MasterPageFile = "~/Account/NewAccountMasterPage.Master";
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string userId = Request.QueryString["UserId"];
            Guid idUser = new Guid();
            using (wsAteneoLibri.AteneoLibriServiceClient service = new wsAteneoLibri.AteneoLibriServiceClient())
            {
                
                if (userId != null && userId != "")
                {
                    idUser = new Guid(userId);
                    User user = service.getUserById(idUser);

                    lblUserName.Text = user.Username;
                    if (user.UniversityId != null)
                    {
                        lblUniversity.Text = service.getUniversity(user.UniversityId.Value).Name;
                    }
                    if (user.FacultyId != null)
                    {
                        lblFaculty.Text = service.getFaculty(user.FacultyId.Value).Name;
                    }
                    
                }
                
            }
            LoadPersonalLibrary(idUser);
        }

        private void LoadPersonalLibrary(Guid idUser)
        {
            using (wsAteneoLibri.AteneoLibriServiceClient service = new wsAteneoLibri.AteneoLibriServiceClient())
            {
                List<Book> myBooks = service.getBooksByIdUser(idUser);
                List<Book> myLibrary = new List<Book>();

                if (myBooks.Count() > 0)
                {
                    foreach (var item in myBooks)
                    {
                        bool isBookSold = service.getSalesByBookId(item.IdBook).Where(x => x.EndDate != null).Count() > 0;
                        if (!isBookSold) // se il libro non è stato venduto
                        {
                            myLibrary.Add(item);
                        }
                    }

                    int trimTitleLength = Convert.ToInt16(System.Configuration.ConfigurationManager.AppSettings["TrimTitleValue"]);

                    myLibrary = myLibrary.Select(b => new Book { IdBook = b.IdBook, Title = Utility.Utility.TrimTitle(b.Title, trimTitleLength), Author = b.Author, SellingPrice = b.SellingPrice, UserName = b.UserName, InsertDate = b.InsertDate }).OrderByDescending(x => x.InsertDate).ToList();


                    dlLibrary.DataSource = myLibrary;
                    dlLibrary.DataBind();

                    dlLibraryPortrait.DataSource = myLibrary;
                    dlLibraryPortrait.DataBind();
                }
                else
                {
                    pnlNoBooks.Visible = true; // TODO Rivedere
                }
                
            }
        }

        protected void btnDetails_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton button = sender as ImageButton;
            string idBook = button.CommandArgument;
            Response.Redirect("~/BookDetails.aspx?IdBook=" + idBook);
        }

    }

}